package one.stand.service.sys;

import one.stand.dto.DictDto;
import one.stand.mapper.SysDictValueMapperExt;
import one.stand.model.SysDictValue;
import one.stand.util.OneListUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author cjr
 */
@Service
public class SysDictValueServiceImpl implements SysDictValueService {
    @Autowired
    private SysDictValueMapperExt sysDictValueMapperExt;

    @Override
    public DictDto get2(Type type, String name) {
        SysDictValue sysDictValue = get(type, name);
        return Objects.nonNull(sysDictValue) ? DictDto.builder()
                .id(sysDictValue.getId())
                .dictId(sysDictValue.getDictId())
                .type(sysDictValue.getType())
                .name(sysDictValue.getName())
                .value(sysDictValue.getValue())
                .build() : null;
    }

    @Override
    public DictDto get2(Type type, TypeName name) {
        SysDictValue sysDictValue = get(type, name);
        return Objects.nonNull(sysDictValue) ? DictDto.builder()
                .id(sysDictValue.getId())
                .dictId(sysDictValue.getDictId())
                .type(sysDictValue.getType())
                .name(sysDictValue.getName())
                .value(sysDictValue.getValue())
                .build() : null;
    }

    @Override
    public SysDictValue get(Type type, TypeName name) {
        return get(type, name.name());
    }

    @Override
    public SysDictValue get(Type type, String name) {
        return get(type, DEFAULT_COMPANY_KEY, name);
    }

    @Override
    public SysDictValue getNullDefault(Type type, String companyKey, String name) {
        if (StringUtils.isBlank(companyKey)) return get(type, name);

        SysDictValue sysDictValue = get(type, companyKey, name);
        if (Objects.isNull(sysDictValue)) {
            return get(type, name);
        }
        return sysDictValue;
    }

    @Override
    public SysDictValue get(Type type, String companyKey, String name) {
        return sysDictValueMapperExt.selectByTypeName(type.name(), companyKey, name);
    }

    @Override
    public List<SysDictValue> getList(Type type, String companyKey) {
        return sysDictValueMapperExt.selectList(type.name(), companyKey);
    }

    @Override
    public Map<String, SysDictValue> getMap(Type type, String companyKey) {
        List<SysDictValue> sysDictValues = getList(type, companyKey);
        if (OneListUtil.isNotEmpty(sysDictValues)) {
            return sysDictValues.stream().collect(Collectors.toMap(SysDictValue::getName, dict -> dict));
        }
        return null;
    }

    @Override
    public Map<String, SysDictValue> getMapNullDefault(Type type, String companyKey) {
        Map<String, SysDictValue> map = getMap(type, companyKey);
        if (Objects.nonNull(map)) {
            return map;
        }
        return getMap(type, DEFAULT_COMPANY_KEY);
    }

    @Override
    public boolean isOn(Type type, TypeName name) {
        return isOn(type, name.name());
    }

    @Override
    public boolean isOn(Type type, String name) {
        SysDictValue sysDictValue = get(type, name);
        return Objects.nonNull(sysDictValue);
    }
}
